package cc.cix.hq.common.core.validator;

import cc.cix.hq.common.core.annotation.IsMobile;
import cc.cix.hq.common.core.entity.constant.RegexpConstant;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 校验是否为合法的手机号码
 *
 * @author cix
 */
public class MobileValidator implements ConstraintValidator<IsMobile, String> {

    @Override
    public void initialize(IsMobile isMobile) {
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        try {
            if (StrUtil.isBlank(s)) {
                return true;
            } else {
                return ReUtil.isMatch(RegexpConstant.MOBILE, s);
            }
        } catch (Exception e) {
            return false;
        }
    }
}
